package com.greateffect.littlebud.lib.utils;

import android.support.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;

/**
 * Created by ZengCS on 2019/3/15.
 * function：百分比转换
 */
public class NumberUtils {
    //保留小数位数
    public final static int DECIMAL_0 = 0;
    public final static int DECIMAL_1 = 1;
    public final static int DECIMAL_2 = 2;
    public final static int DECIMAL_DEFAULT = -1;

    @IntDef({DECIMAL_DEFAULT,DECIMAL_0, DECIMAL_1,DECIMAL_2})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Decimal {}

    /**
     * 小数转化成百分百，并保留指定小数位数
     * @param value [0 ~  1]
     * @param decimal
     * @return
     */
    public static String getRatio2Percent(Float value, @Decimal int decimal){
        if(value == null){
            return "0%";
        }
        float newValue = value * 100;
        String result = newValue + "%";
        switch (decimal){
            case DECIMAL_0:
                result = String.format(Locale.CHINESE,"%d%s",(int)(newValue),"%");
                break;
            case DECIMAL_1:
                result = String.format(Locale.CHINESE,"%.1f%s",newValue,"%");
                break;
            case DECIMAL_2:
                result = String.format(Locale.CHINESE,"%.2f%s",newValue,"%");
                break;
            case DECIMAL_DEFAULT:
                result = String.format(Locale.CHINESE,"%f%s",newValue,"%");
                break;
        }

        return result;
    }

    /**
     * 小数转化成百分百，并保留指定小数位数
     * @param value [0 ~  1]
     * @param decimal
     * @return
     */
    public static String getDecimalNumber(Float value, @Decimal int decimal){
        if(value == null){
            return "0";
        }
        String result = String.valueOf(value);
        switch (decimal){
            case DECIMAL_0:
                result = String.format(Locale.CHINESE,"%d",value.intValue());
                break;
            case DECIMAL_1:
                result = String.format(Locale.CHINESE,"%.1f",value);
                break;
            case DECIMAL_2:
                result = String.format(Locale.CHINESE,"%.2f",value);
                break;
            case DECIMAL_DEFAULT:
                result = String.format(Locale.CHINESE,"%f",value);
                break;
        }

        return result;
    }
}
